<?php
include '../includes/variables.php';
include '../includes/cDatabase.php';
include '../includes/cMember.php';
include '../includes/cPlayer.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Add Member");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();
		
if(@$_GET['form'] == "yes")
{
	$error = 0;
	$totalValue = 0;
	// Database defaults
	$moneyPaid = 0;
	$active = 1;
	$defaultWeekIn = 1;
	
	$memberName = stripSlashes($_POST['memberName']);
	$memberTeam = stripSlashes($_POST['memberTeam']);
	$miniPop = stripSlashes($_POST['miniPop']);

	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	
	$cMember = new member;
		
	for ($i = 1; $i <= PLAYERSPERTEAM; $i++)
	{
		$variable = getPlayerCodeVariable($i);
		
		$memberPlayer[$i] = new player($_POST[$variable]);
			
		$error |= $memberPlayer[$i]->setPlayerPosition($connect);
		$error |= $memberPlayer[$i]->setPlayerValue($connect);
		
		$error |= $cMember->addFormation( $memberPlayer[$i]->getPlayerPosition() );
		$error |= $cMember->addPlayerValue( $memberPlayer[$i]->getPlayerValue() );
	}
	
	// Do checks
	if ( ($length = strlen($memberName) ) > MAXMEMBERNAME)
	{
		echo"<BR>Error detected for Member Name:  length exceeds 30 received length ".$length;
		$error |= 1;
	}
	if ( ($length = strlen($memberTeam) ) > MAXTEAMNAME)
	{
		echo"<BR>Error detected for Member Team: length exceeds 30 received length  ".$length;
		$error |= 1;
	}
	$error |= $cMember->checkFormation();
	
	if (bcsub(MAXVALUE, $cMember->getMemberTotalScore(), 1) < 0)	// To avoid floating point inaccuracies
	{
		$error |= 1;
		echo"<BR>Team value ".$cMember->getMemberTotalScore()."m exceeds maximum value ".MAXVALUE."m";
	}
	
	if ($captain == NULL)
	{
		$error |= 1;
		echo"<BR>Captain not selected";
	}
	
	$maxWeeks = $cDatabase->GetMaxWeeks($connect);
	
	if (!$error)
	{
		$cDatabase->insertMember($maxWeeks, $connect, $memberName, $memberTeam, $moneyPaid, $miniPop,
			$memberPlayer[1]->getPlayerCode(),$memberPlayer[2]->getPlayerCode(),$memberPlayer[3]->getPlayerCode(),
			$memberPlayer[4]->getPlayerCode(),$memberPlayer[5]->getPlayerCode(),$memberPlayer[6]->getPlayerCode(),$memberPlayer[7]->getPlayerCode(),
			$memberPlayer[8]->getPlayerCode(),$memberPlayer[9]->getPlayerCode(),$memberPlayer[10]->getPlayerCode(),$memberPlayer[11]->getPlayerCode(),
			$memberPlayer[$captain]->getPlayerCode(), $userName, $userPass
		);
	}
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes" method="POST">
  <table border='0'>
    <tr>
      <td>Name</td>
      <td>Team Name</td>
    </tr>
    <tr>
      <td><input type='text' name='memberName' size='20' maxlength='20' value=<?php echo @$_POST['memberName'] ?>></td>
      <td><input type='text' name='memberTeam' size='50' maxlength='50' value=<?php echo @$_POST['memberTeam'] ?>></td>
	</tr>
	<tr>
      <td>Username (Ericsson signum with team number on the end)</td>
      <td>Password (Same as username)</td>
    </tr>
	<tr>
	  <td><input type='text' name='userName' size='20' maxlength='20' value=<?php echo @$_POST['userName'] ?>></td>
      <td><input type='text' name='userPass' size='50' maxlength='50' value=<?php echo @$_POST['password'] ?>></td>
    </tr>
	<tr><td>&nbsp</td></tr>
	<tr><td>Minipop</td>
	<td><input type='radio' name='miniPop' value='1'>1
	<input type='radio' name='miniPop' value='2'>2
	<input type='radio' name='miniPop' value='3'>3
	<input type='radio' name='miniPop' value='4'>4
	<input type='radio' name='miniPop' value='5'>5
	<input type='radio' name='miniPop' value='6'>6
	<input type='radio' name='miniPop' value='7'>7
	<input type='radio' name='miniPop' value='8'>8</td>
	</tr>
	<tr><td>&nbsp</td></tr>
	<tr><td>Captain</td></tr>
	<tr><td><input type='radio' name='captain' value='1'></td>
	<td>Player 01</td><td><input type='text' name='playerCode01' size='3' maxlength='4' value=<?php echo @$_POST['playerCode01'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='2'></td>
	<td>Player 02</td><td><input type='text' name='playerCode02' size='3' maxlength='4' value=<?php echo @$_POST['playerCode02'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='3'></td>
	<td>Player 03</td><td><input type='text' name='playerCode03' size='3' maxlength='4' value=<?php echo @$_POST['playerCode03'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='4'></td>
	<td>Player 04</td><td><input type='text' name='playerCode04' size='3' maxlength='4' value=<?php echo @$_POST['playerCode04'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='5'></td>
	<td>Player 05</td><td><input type='text' name='playerCode05' size='3' maxlength='4' value=<?php echo @$_POST['playerCode05'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='6'></td>
	<td>Player 06</td><td><input type='text' name='playerCode06' size='3' maxlength='4' value=<?php echo @$_POST['playerCode06'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='7'></td>
	<td>Player 07</td><td><input type='text' name='playerCode07' size='3' maxlength='4' value=<?php echo @$_POST['playerCode07'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='8'></td>
	<td>Player 08</td><td><input type='text' name='playerCode08' size='3' maxlength='4' value=<?php echo @$_POST['playerCode08'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='9'></td>
	<td>Player 09</td><td><input type='text' name='playerCode09' size='3' maxlength='4' value=<?php echo @$_POST['playerCode09'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='10'></td>
	<td>Player 10</td><td><input type='text' name='playerCode10' size='3' maxlength='4' value=<?php echo @$_POST['playerCode10'] ?>></td></tr>
	<tr><td><input type='radio' name='captain' value='11'></td>
	<td>Player 11</td><td><input type='text' name='playerCode11' size='3' maxlength='4' value=<?php echo @$_POST['playerCode11'] ?>></td></tr>
	</table>
	<table>
	<td colspan="2" align="center">
		<input name="addMemberButton" type="submit" id="addMember" value="Add Member">
	</td>
</table>
</form>
<?php
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>